package cn.wolfcode.luowowo.comment.services.impl;

import cn.wolfcode.luowowo.comment.domain.Wenda;
import cn.wolfcode.luowowo.comment.domain.WendaComment;
import cn.wolfcode.luowowo.comment.repository.WendaCommentRepository;
import cn.wolfcode.luowowo.comment.repository.WendaMongoRepository;
import cn.wolfcode.luowowo.comment.service.IWendaCommentService;
import cn.wolfcode.luowowo.comment.service.IWendaService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.lang.reflect.Array;
import java.util.*;

@Service
public class WendaServiceImpl implements IWendaService {

    @Autowired
    private WendaMongoRepository wendaMongoRepository;
    @Reference
    private IUserInfoService userInfoService;
    @Autowired
    private WendaCommentRepository wendaCommentRepository;


    @Override
    public List<Wenda> listAll() {
        List<Wenda> list = wendaMongoRepository.findAll(new Sort(Sort.Direction.DESC, "createTime"));
        return list;
    }

    @Override
    public AjaxResult save(Wenda wenda) {
        AjaxResult result = new AjaxResult();
        if (wenda.getCreateTime() == null) {
            Date date = new Date();
            wenda.setCreateTime(date);
        }
        try {
            wendaMongoRepository.save(wenda);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("社区提问失败");
        }
        return result;
    }

    @Override
    public Wenda getWenda(String wendaId) {
        Optional<Wenda> op = wendaMongoRepository.findById(wendaId);
        Wenda wenda = op.get();
        return wenda;
    }

    @Override
    public AjaxResult favor(String wendaId, UserInfo userInfo) {
        AjaxResult result = new AjaxResult();
        Optional<Wenda> byId = wendaMongoRepository.findById(wendaId);
        Wenda wenda = byId.get();
        List<Long> list = wenda.getInteres();
        if (userInfo == null) {
            result.setData(list.size());
            result.mark("请先登录");
            return result;
        }
        for (Long l : list) {
            if (l.equals(userInfo.getId())) {
                result.mark("取消关注");
                list.remove(userInfo.getId());
                result.setData(list.size());
                //取消关注后,修改关注的人的数量
                wenda.setInteresnum(list.size());
                wendaMongoRepository.save(wenda);
                return result;
            }
        }
        list.add(userInfo.getId());
        result.setData(list.size());
        //关注成功后,修改关注的人的数量
        wenda.setInteresnum(list.size());
        wendaMongoRepository.save(wenda);
        return result;
    }

    @Override
    public boolean isFavor(String objectId, Long userId) {
        Wenda wenda = getWenda(objectId);
        List<Long> users = wenda.getInteres();
        boolean contains = users.contains(userId);
        return contains;
    }

    @Override
    public List<Long> listTop10(int rankingId) {
        List<Long> list = new ArrayList<>();
        switch (rankingId) {
            //排行榜:回答:提问回答最多的用户
            case 1:
                List<Wenda> wendas1 = wendaMongoRepository.findAll(new Sort(Sort.Direction.DESC, "answernum"));
                int num = 0;

                for (Wenda wenda : wendas1) {
                    list.add(wenda.getUserId());

                    UserInfo userInfo = userInfoService.get(wenda.getUserId());

                    if (wenda.getAnswernum() != null && wenda.getAnswernum() != 0) {
                        num += wenda.getAnswernum();
                    }

                    userInfo.setRankingNum(num);
                    userInfoService.updateRanking(userInfo);


                }

                break;
            //排行榜:金牌:回答金牌最多的用户
            case 0:
                List<Wenda> wendas3 = wendaMongoRepository.findAll();
                List<WendaComment> wendaComments = wendaCommentRepository.findAll();

                for (WendaComment wendaComment : wendaComments) {
                    if (wendaComment.getGoldAnswer() == 1) {
                        if (!list.contains(wendaComment.getUserId())) {
                            list.add(wendaComment.getUserId());
                        }
                    }
                }
                int goldAnswerNum = 0;
                for (Wenda wenda : wendas3) {
                    UserInfo userInfo = new UserInfo();
                    for (int i = 0; i < list.size(); i++) {


                        for (Long aLong : wenda.getGoldAnswerList()) {
                            userInfo = userInfoService.get(aLong);
                            if (aLong == list.get(i)) {
                                goldAnswerNum += 1;
                            }


                        }

                    }
                    userInfo.setRankingNum(goldAnswerNum);
                    userInfoService.updateRanking(userInfo);
                }
                break;
            //排行榜:点赞:提问点赞最多的用户
            case 2:
                List<Wenda> wendas4 = wendaMongoRepository.findAll(new Sort(Sort.Direction.ASC, "thumbupnum"));
                int tNum = 0;
                for (Wenda wenda : wendas4) {
                    list.add(wenda.getUserId());
                    UserInfo info = userInfoService.get(wenda.getUserId());

                    tNum += wenda.getThumbupnum();
                    info.setRankingNum(tNum);

                    userInfoService.updateRanking(info);
                }
                break;
        }
        for (Long userId : list) {
            if (!list.contains(userId)) {
                list.add(userId);
            }
        }
        return list;
    }

}
